/*
 * @FileName: [UserController.java]
 * 
 * @Package controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO Technology Limited Company ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 2909 $ $LastChangedDate: 2013-10-12 10:19:36 +0800 (周六, 2013-10-12) $ $LastChangedBy: fanb $
 * 
 * @category bcinfo
 * 
 * @version 1.1
 * 
 * @author $Author: fanb $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bci.pwtz.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.admin.service.AdminCompanyService;
import com.bci.pwtz.admin.service.AdminSmsSendLogService;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.AdminSmsSendLog;
import com.bci.pwtz.mysql.model.Company;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.GeoCode;
import com.bci.pwtz.service.CompanyUserService;
import com.bci.pwtz.service.GeoCodeService;
import com.bci.pwtz.service.sms.SendSmsService;



/**
 * 用户控制器controller <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 包含以下功能 <br/>
 * 登陆 <br/>
 * 
 * <PRE>
 *      /user/${actionName}
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/admin/company")
public class AdminCompanyController extends AdminAbstractController
{
	@Autowired
	private AdminCompanyService adminCompanyService;
	@Autowired
    private GeoCodeService geoCodeService;
	@Autowired
	private SendSmsService sendSmsService;
	@Autowired
	private CompanyUserService companyUserService;
	@Autowired
	private AdminSmsSendLogService smsSendLogService;
	
	
	@RequestMapping(value="/geo",method=RequestMethod.POST)
    @ResponseBody
    public List<GeoCode>  getGeoByParent(Long parent){
        List<GeoCode> list = new ArrayList<GeoCode>();
        try
        {
            list=geoCodeService.findGeoCodeList(parent);
        }
        catch (PwtzDatabaseException e)
        {
            e.printStackTrace();
        }
        return list;
    }
    
	@RequestMapping("/list")
	@ResponseBody
	public PaginationList<Company> list(Company bean,String beginTime,String endTime,int pageNo,int pageSize){
		try {
			return adminCompanyService.findCompany(bean, beginTime, endTime, pageNo, pageSize);
		} catch (PwtzDatabaseException e) {
			handleException(e);
		}
		return null;
	}
	
	@RequestMapping("/load")
	@ResponseBody
	public Company load(Long id){
		try {
			return adminCompanyService.load(id);
		} catch (PwtzDatabaseException e) {
			handleException(e);
		}
		return null;
	}
	
	@RequestMapping("/approvePay")
	@ResponseBody
	public Map<String, String> approvePay(Long companyId,String invalidTime){
		try {
			Date time = PwtzUtils.parseDate(invalidTime);
			if(time == null || "".equals(time)){
				return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "请填写付费有效期。");
			}
			if(new Date().after(time)){
				return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "付费有效期不能小于当前时间。");
			}
			
			adminCompanyService.approvePay(companyId, time);
			Company company = adminCompanyService.load(companyId);
			setLog(2, "单位【"+company.getCompanyName()+"】付费成功", getUserId());
			CompanyUser user = companyUserService.load(company.getCreatorId());
			String message = "您的公司【"+company.getCompanyName()+"】已经付费成功。";
			String smsReturn = sendSmsService.batchSend(user.getMobile().split(","), message, null, null);
    		if("0".equals(smsReturn) || "1".equals(smsReturn)){
    			AdminSmsSendLog smsSendLog = new AdminSmsSendLog();
    			smsSendLog.setMobile(user.getMobile());
    			smsSendLog.setContent(message);
    			smsSendLog.setOperatorId(getUserId());
    			smsSendLogService.add(smsSendLog);
    		}
			return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_ERROR,null, "付费成功。");
		} catch (Exception e) {
			handleException(e);
		}
		return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,null, "付费失败。");
	}
	
	@RequestMapping("/statistics")
    @ResponseBody
    public List<Company> statistics(String provinceCode,String cityCode,String beginTime,String endTime){
        try {
            return adminCompanyService.selectcCompanyStatistics(provinceCode, cityCode, beginTime, endTime);
        } catch (PwtzDatabaseException e) {
            handleException(e);
        }
        return null;
    }
}